<template>
  <template v-if="rowInfo.id">
    <q-item class="justify-around">
      <q-btn outline text-color="primary" unelevated @click="handleDispatch">
        {{ $t('TaskCore.ManualDispatch') }}
      </q-btn>
      <q-btn :disable="isDisable" outline text-color="primary" unelevated @click="handleReview">
        {{ $t('TaskCore.ManualReview') }}
      </q-btn>
      <q-btn :disable="isDisable" outline text-color="primary" unelevated @click="handleComplete">
        {{ $t('TaskCore.ManualComplete') }}
      </q-btn>
    </q-item>
    <QInfo :columns="columns" :rowInfo="rowInfo" cols="4"></QInfo>
  </template>
  <MEmpty v-else classes="q-my-md"></MEmpty>
</template>
<script setup>
import { i18n } from 'boot/i18n'
import { postAPI } from 'src/api'
import { AGVTypes, taskStatus } from 'src/enum'
import { createDialog } from 'src/utils/utils'
import { computed, ref, watch } from 'vue'

const props = defineProps({ taskCode: String })
const modalVisible = defineModel('modelValue', { type: Boolean, default: false })

const rowInfo = ref({})
const isDisable = computed(() => {
  if (rowInfo.value.taskStatus === '8' || rowInfo.value.taskStatus === '6') return true
  return rowInfo.value.sysInner === '1'
})
const columns = computed(() => {
  return [{
    label: `${i18n.t('AGVCore.AGVBrand')}`, field: 'agvBrandCode', minWidth: 80
  }, {
    label: `${i18n.t('TaskCore.TaskGroupCode')}`, field: 'taskGroupCode', minWidth: 80
  }, {
    label: `${i18n.t('TaskCore.TaskStatus')}`, field: 'taskStatus', options: taskStatus.value,
    minWidth: 80, useTag: true
  }, {
    label: `${i18n.t('AGVCore.AGVType')}`, field: 'agvTypeCode', type: 'select', options: AGVTypes.value,
    minWidth: 80, search: false
  }, {
    label: `${i18n.t('Misc.WaitReason')}`, field: 'waitReason', minWidth: 80, search: false, hide: true
  }, {
    label: `${i18n.t('Misc.ExceptionReason')}`, field: 'exceptionMsg', minWidth: 80, search: false, hide: true
  }, {
    label: `${i18n.t('MapCore.StartStation')}`, field: 'startPositionCode', minWidth: 80
  }, {
    label: `${i18n.t('MapCore.EndStation')}`, field: 'endPositionCode', minWidth: 80
  }, {
    label: `${i18n.t('MapCore.StartArea')}`, field: 'startAreaCode', minWidth: 80, search: false, hide: true
  }, {
    label: `${i18n.t('MapCore.EndArea')}`, field: 'endAreaCode', minWidth: 80, search: false, hide: true
  }, {
    label: `${i18n.t('CarrierCore.StartRackCode')}`, field: 'startShelvesCode', minWidth: 80,
    search: false, hide: true
  }, {
    label: `${i18n.t('CarrierCore.EndRackCode')}`, field: 'endShelvesCode', minWidth: 80, search: false, hide: true
  }, {
    label: `${i18n.t('CarrierCore.StartSlotCode')}`, field: 'startStorageCode', minWidth: 80,
    search: false, hide: true
  }, {
    label: `${i18n.t('CarrierCore.EndSlotCode')}`, field: 'endStorageCode', minWidth: 80, search: false, hide: true
  }, {
    label: `${i18n.t('AGVCore.AGVCode')}`, field: 'agvCode', minWidth: 80
  }, {
    label: `${i18n.t('CarrierCore.RackCode')}`, field: 'shelvesCode', minWidth: 80, hide: true
  }, {
    label: `${i18n.t('CarrierCore.ContainerCode')}`, field: 'containerCode', minWidth: 80,
    search: false, hide: true
  }, {
    label: `${i18n.t('CreationTime')}`, field: 'createdTime', type: 'daterange', minWidth: 80
  }, {
    label: `${i18n.t('Misc.ScheduleTime')}`, field: 'execDispatchTime', minWidth: 80, search: false
  }, {
    label: `${i18n.t('StartTime')}`, field: 'execBeginTime', minWidth: 80, search: false
  }, {
    label: `${i18n.t('Misc.ExitTime')}`, field: 'execOutTime', minWidth: 80, search: false
  }, {
    label: `${i18n.t('EndTime')}`, field: 'execEndTime', minWidth: 80, search: false
  }, {
    label: `${i18n.t('TaskCore.TaskTemplateCode')}`, field: 'rcsTaskType', minWidth: 80
  }]
})

const handleDispatch = () => {
  createDialog({ msg: `${i18n.t('Sundry.DispatchTips')}` }, () => {
    postAPI('/yfagv/api/execL3TcrSplitTask/manualScheduling', {}, () => initData())
  })
}
const handleComplete = () => {
  const { taskCode: splitTaskCode } = rowInfo.value
  createDialog({ msg: `${i18n.t('Sundry.CompleteTips')}` }, () => {
    postAPI('/yfagv/api/execL3TcrSplitTask/manuallyFinishSplitTask', { remark: '', splitTaskCode }, () => initData())
  })
}
const handleReview = () => {
  const params = {}
  params.splitTaskCode = rowInfo.value.taskCode
  createDialog({ msg: `${i18n.t('Sundry.ReviewTips')}` }, () => {
    postAPI('/yfagv/api/execL3TcrSplitTask/reviewSplitTask', params, () => initData())
  })
}
const initData = () => {
  if (!props.taskCode) return
  postAPI('/yfagv/api/execL3TcrSplitTask/list', { taskCode: props.taskCode }, ({ rows = [] }) => {
    if (rows.length) rowInfo.value = rows[0]
  })
}
watch(modalVisible, val => val && initData(), { immediate: true })
</script>